home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 681 / term / source.lha / termXEM.c < prev    next >
C/C++ Source or Header  |  1992-05-09  |  7KB  |  360 lines

  1. /*
  2. **    $Id: termXEM.c,v 1.3 92/04/21 16:58:10 olsen Sta Locker: olsen $
  3. **    $Revision: 1.3 $
  4. **    $Date: 92/04/21 16:58:10 $
  5. **
  6. **    External emulation support routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* xem_swrite():
  15.      *
  16.      *    Send a few bytes across the serial line.
  17.      */
  18.  
  19. STATIC LONG __saveds __asm
  20. xem_swrite(register __a0 UBYTE *Buffer,register __d0 LONG Size)
  21. {
  22.     if(WriteRequest)
  23.     {
  24.         SerWrite(Buffer,Size);
  25.  
  26.         return(0);
  27.     }
  28.     else
  29.         return(-1);
  30. }
  31.  
  32.     /* xem_sbreak():
  33.      *
  34.      *    Send a break signal across the serial line.
  35.      */
  36.  
  37. STATIC BOOL __saveds
  38. xem_sbreak()
  39. {
  40.     if(!WriteRequest)
  41.         return(-1);
  42.     else
  43.     {
  44.         WriteRequest -> IOSer . io_Command = SDCMD_BREAK;
  45.  
  46.         return((LONG)DoIO(WriteRequest));
  47.     }
  48. }
  49.  
  50.     /* xem_sstart():
  51.      *
  52.      *    Restart serial read activity.
  53.      */
  54.  
  55. STATIC VOID __saveds
  56. xem_sstart()
  57. {
  58.     if(ReadRequest)
  59.     {
  60.         ReadRequest -> IOSer . io_Command    = CMD_READ;
  61.         ReadRequest -> IOSer . io_Data        = ReadBuffer;
  62.         ReadRequest -> IOSer . io_Length    = 1;
  63.  
  64.         SetSignal(0,SIG_SERIAL);
  65.  
  66.         SendIO(ReadRequest);
  67.     }
  68. }
  69.  
  70.     /* xem_sstop():
  71.      *
  72.      *    Stop serial read activity.
  73.      */
  74.  
  75. STATIC VOID __saveds
  76. xem_sstop()
  77. {
  78.     if(ReadRequest)
  79.     {
  80.         if(!CheckIO(ReadRequest))
  81.             AbortIO(ReadRequest);
  82.  
  83.         WaitIO(ReadRequest);
  84.     }
  85. }
  86.  
  87.     /* xem_tgets(UBYTE *Prompt,UBYTE *Buffer,ULONG Size):
  88.      *
  89.      *    Get a string from the user.
  90.      */
  91.  
  92. STATIC BOOL __saveds __asm
  93. xem_tgets(register __a0 UBYTE *Prompt,register __a1 UBYTE *Buffer,register __d0 ULONG Size)
  94. {
  95.     return((BOOL)xpr_gets(Prompt,Buffer));
  96. }
  97.  
  98.     /* xem_tbeep(ULONG Times,ULONG Delay):
  99.      *
  100.      *    Beep the terminal display.
  101.      */
  102.  
  103. STATIC VOID __saveds __asm
  104. xem_tbeep(register __d0 ULONG Times,register __d1 ULONG Delay)
  105. {
  106.     WORD i;
  107.  
  108.     for(i = 0 ; i < Times ; i++)
  109.     {
  110.             /* Handle the visual part. */
  111.  
  112.         if(Config . VisibleBell)
  113.         {
  114.             if(StatusProcess)
  115.                 Signal(StatusProcess,SIGBREAKF_CTRL_D);
  116.         }
  117.  
  118.             /* Let it beep. */
  119.  
  120.         if(Config . AudibleBell)
  121.             Beep();
  122.     }
  123. }
  124.  
  125.     /* xem_macrodispatch(struct XEmulatorMacroKey *XEM_MacroKey):
  126.      *
  127.      *    Dispatch a macro key call.
  128.      */
  129.  
  130. STATIC VOID __saveds __asm
  131. xem_macrodispatch(register __a0 struct XEmulatorMacroKey *XEM_MacroKey)
  132. {
  133.     VOID (*Routine)(VOID);
  134.  
  135.         /* If a routine to call is available (most likely xON or xOFF),
  136.          * make a call to it, else process the macro key data.
  137.          */
  138.  
  139.     if(Routine = XEM_MacroKey -> xmk_UserData)
  140.         (*Routine)();
  141.     else
  142.         SerialCommand(MacroKeys -> Keys[XEM_MacroKey -> xmk_Qualifier][XEM_MacroKey -> xmk_Code - 0x50]);
  143. }
  144.  
  145.     /* SetupEmulator(BYTE OpenConsole):
  146.      *
  147.      *    Initialize the XEM_IO structure.
  148.      */
  149.  
  150. STATIC BYTE __regargs
  151. SetupEmulator(VOID)
  152. {
  153.     if(!XEM_IO)
  154.     {
  155.         if(XEM_IO = (struct XEM_IO *)AllocVec(sizeof(struct XEM_IO),MEMF_ANY|MEMF_CLEAR))
  156.         {
  157.             XEM_IO -> xem_window        = Window;
  158.             XEM_IO -> xem_font        = CurrentFont;
  159.             XEM_IO -> xem_signal        = &XEM_Signal;
  160.             XEM_IO -> xem_string        = "";
  161.             XEM_IO -> xem_screendepth    = Screen -> RastPort . BitMap -> Depth;
  162.  
  163.             XEM_IO -> xem_sread        = (APTR)xpr_sread;
  164.             XEM_IO -> xem_swrite        = (APTR)xem_swrite;
  165.             XEM_IO -> xem_sflush        = (APTR)xpr_sflush;
  166.             XEM_IO -> xem_sbreak        = (APTR)xem_sbreak;
  167.             XEM_IO -> xem_squery        = (APTR)xpr_squery;
  168.             XEM_IO -> xem_sstart        = (APTR)xem_sstart;
  169.             XEM_IO -> xem_sstop        = (APTR)xem_sstop;
  170.  
  171.             XEM_IO -> xem_tbeep        = (APTR)xem_tbeep;
  172.             XEM_IO -> xem_tgets        = (APTR)xem_tgets;
  173.             XEM_IO -> xem_toptions        = (APTR)xpr_options;
  174.  
  175.             XEM_IO -> xem_process_macrokeys    = (APTR)xem_macrodispatch;
  176.  
  177.             return(TRUE);
  178.         }
  179.     }
  180.     else
  181.         return(FALSE);
  182.  
  183.     return(FALSE);
  184. }
  185.  
  186.     /* CloseEmulator():
  187.      *
  188.      *    Close the emulation library.
  189.      */
  190.  
  191. VOID
  192. CloseEmulator()
  193. {
  194.     if(XEmulatorBase)
  195.     {
  196.         if(XEM_IO)
  197.         {
  198.             XEmulatorMacroKeyFilter(XEM_IO,NULL);
  199.             XEmulatorCloseConsole(XEM_IO);
  200.             XEmulatorCleanup(XEM_IO);
  201.  
  202.             FreeVec(XEM_IO);
  203.  
  204.             XEM_IO = NULL;
  205.         }
  206.  
  207.         CloseLibrary(XEmulatorBase);
  208.  
  209.         strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  210.  
  211.         XEmulatorBase    = NULL;
  212.         XEM_Signal    = NULL;
  213.  
  214.         SetCursor();
  215.     }
  216. }
  217.  
  218.     /* OpenEmulator(UBYTE *Name):
  219.      *
  220.      *    Open an emulation library.
  221.      */
  222.  
  223. BYTE
  224. OpenEmulator(UBYTE *Name)
  225. {
  226.     CloseEmulator();
  227.  
  228.     XEM_HostData . Source        = NULL;
  229.     XEM_HostData . Destination    = NULL;
  230.     XEM_HostData . InESC        = FALSE;
  231.     XEM_HostData . InCSI        = FALSE;
  232.  
  233.     if(XEmulatorBase = OpenLibrary(Name,0))
  234.     {
  235.         if(SetupEmulator())
  236.         {
  237.             if(Config . RasterEnabled)
  238.             {
  239.                 EraseScreen("2");
  240.  
  241.                 SetWrMsk(RPort,(1 << Screen -> RastPort . BitMap -> Depth) - 1);
  242.             }
  243.  
  244.             ClearCursor();
  245.  
  246.             if(XEmulatorSetup(XEM_IO))
  247.             {
  248.                 if(XEmulatorOpenConsole(XEM_IO))
  249.                 {
  250.                     UBYTE *LibName = FilePart(Name);
  251.  
  252.                     strcpy(EmulationName,&LibName[3]);
  253.  
  254.                     EmulationName[strlen(EmulationName) - 8] = 0;
  255.  
  256.                     SetupXEM_MacroKeys(MacroKeys);
  257.  
  258.                     return(TRUE);
  259.                 }
  260.             }
  261.  
  262.             SetCursor();
  263.         }
  264.  
  265.         CloseLibrary(XEmulatorBase);
  266.  
  267.         strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  268.  
  269.         XEmulatorBase    = NULL;
  270.         XEM_Signal    = NULL;
  271.     }
  272.  
  273.     return(FALSE);
  274. }
  275.  
  276.     /* XOff():
  277.      *
  278.      *    Small local routine, complements XOn() in Serial.c
  279.      */
  280.  
  281. STATIC VOID
  282. XOff()
  283. {
  284.     if(Status == STATUS_HOLDING)
  285.     {
  286.         UBYTE c = XOF;
  287.  
  288.         SerWrite(&c,1);
  289.  
  290.         Status = STATUS_READY;
  291.     }
  292. }
  293.  
  294.     /* SetupXEM_MacroKeys(struct MacroKeys *Keys):
  295.      *
  296.      *    Sets up the internal representation of the macro key
  297.      *    data to fit the XEM specification.
  298.      */
  299.  
  300. VOID
  301. SetupXEM_MacroKeys(struct MacroKeys *Keys)
  302. {
  303.         /* Are we allowed to do what we want to do? */
  304.  
  305.     if(XEM_MacroKeys && XEmulatorBase && Config . Emulation == EMULATION_EXTERNAL)
  306.     {
  307.         WORD i,j,k = 0;
  308.  
  309.             /* Clear the macro list. */
  310.  
  311.         NewList(&XEM_MacroList);
  312.  
  313.             /* Run down the list of qualifiers. */
  314.  
  315.         for(i = XMKQ_NONE ; i <= XMKQ_CONTROL ; i++)
  316.         {
  317.                 /* Run down the function keys. */
  318.  
  319.             for(j = 0 ; j < 10 ; j++)
  320.             {
  321.                     /* If the key has no data attached,
  322.                      * don't use it in the list.
  323.                      */
  324.  
  325.                 if(Keys -> Keys[i][j][0])
  326.                 {
  327.                     XEM_MacroKeys[k] . xmk_Type        = XMKT_RAWKEY;
  328.                     XEM_MacroKeys[k] . xmk_Qualifier    = i;
  329.                     XEM_MacroKeys[k] . xmk_Code        = 0x50 + j;
  330.                     XEM_MacroKeys[k] . xmk_UserData        = NULL;
  331.  
  332.                     AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  333.                 }
  334.             }
  335.         }
  336.  
  337.             /* Take care of the rest, add support for the xON key. */
  338.  
  339.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  340.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  341.         XEM_MacroKeys[k] . xmk_Code        = XON;
  342.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)XOn;
  343.  
  344.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  345.  
  346.             /* Take care of the xOFF key. */
  347.  
  348.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  349.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  350.         XEM_MacroKeys[k] . xmk_Code        = XOF;
  351.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)XOff;
  352.  
  353.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k]);
  354.  
  355.             /* Make the emulator notice the new settings. */
  356.  
  357.         XEmulatorMacroKeyFilter(XEM_IO,&XEM_MacroList);
  358.     }
  359. }
  360.